const jwt = require('jsonwebtoken')

function sign(option) {
    return jwt.sign(option, '666', {
        expiresIn: 86400  //一天后过期
    })
}

function verify() {
    return async (ctx, next) => {
        let jwtToken = ctx.req.headers.authorization
        if (jwtToken) {
            //判断token是否合法
            try {
                const decoded = jwt.verify(jwtToken, '666')
                if (decoded.id) {//合法
                    ctx.userId=decoded.id
                     await next()
                }
            } catch (e) {

                ctx.body = {
                    status: 401,
                    msg: 'token失效'
                }
            }
            //  console.log(decoded);
        } else {

            ctx.body = {
                status: 401,
                msg: '请提供token'
            }
        }
    }
}

function getUserId(){
    let jwtToken=ctx.req.headers.authorization
    return jwt.verify(jwtToken,'666')
}
module.exports = {
    sign,
    verify
}